clear all

cd "~\Data"
use "study2_raw.dta"

*BPAQ
recode BPAQ_1 1=6 2=5 3=4 4=3 5=2 6=1
recode BPAQ_3 1=6 2=5 3=4 4=3 5=2 6=1
recode BPAQ_4 1=6 2=5 3=4 4=3 5=2 6=1
recode BPAQ_5 1=6 2=5 3=4 4=3 5=2 6=1
alpha BPAQ*, item casewise asis gen(TraitAgg)

*demos
gen PID=pid
recode PID 1=2 2=6 3/4=4
recode PID 2=1 if pidd==1
recode PID 6=7 if pidr==1
recode PID 4=3 if pidi==1
recode PID 4=5 if pidi==2
gen PIDext=abs(PID-4)
recode PID 1/3=0 4=. 5/7=1, gen(DR)
gen White=0
replace White=1 if race==1
replace White=. if race==.
gen Male=0
replace Male=1 if sex==1
replace Male=. if sex==.

*populism
recode populism_for_3 1/2=1 3=0 4/5=-1, gen(populism_for_3_tri)
recode populism_rev_2 1/2=1 3=0 4/5=-1, gen(populism_rev_2_tri)
tab populism_for_3_tri populism_rev_2_tri, cell

recode populism_full_3 1/2=1 3=0 4/5=-1, gen(populism_full_3_tri)
recode populism_full_5 1/2=1 3=0 4/5=-1, gen(populism_full_5_tri)
tab populism_full_3_tri populism_full_5_tri, cell

recode populism_for* (1=5) (2=4) (4=2) (5=1)
gen pop_for_half=(populism_for_1 + populism_for_2 + populism_for_3)/3
gen pop_rev_half=(populism_rev_1 + populism_rev_2 + populism_rev_3)/3

recode populism_full_1 populism_full_2 populism_full_3 (1=5) (2=4) (4=2) (5=1)
gen pop_for_full=(populism_full_1 + populism_full_2 + populism_full_3)/3
gen pop_rev_full=(populism_full_4 + populism_full_5 + populism_full_6)/3

egen Pop_for=rowfirst(pop_for_half pop_for_full)
egen Pop_rev=rowfirst(pop_rev_half pop_rev_full)

gen Pop6=(Pop_for + Pop_rev)/2

*anti-democratic
recode antidem* (1=5) (2=4) (4=2) (5=1)
alpha antidem*, item casewise gen(Antidem)

*conspiracy
recode conspre* (1=5) (2=4) (4=2) (5=1)
alpha conspre*, item casewise gen(Conspre)

*hostile sexism
recode hs_full_1 1/2=1 3=0 4/5=-1, gen(hs_full_1_tri)
recode hs_full_5 1/2=1 3=0 4/5=-1, gen(hs_full_5_tri)

recode hs_forward* (1=5) (2=4) (4=2) (5=1)
alpha hs_reversed*, item casewise gen(hs_rev_half)
alpha hs_forward*, item casewise gen(hs_for_half)

recode hs_full_1 hs_full_2 hs_full_3 (1=5) (2=4) (4=2) (5=1)
alpha hs_full_1 hs_full_2 hs_full_3, item casewise gen(hs_for_full)
alpha hs_full_4 hs_full_5 hs_full_6, item casewise gen(hs_rev_full)

egen HS_for=rowfirst(hs_for_half hs_for_full)
egen HS_rev=rowfirst(hs_rev_half hs_rev_full)

alpha hs_full_1 hs_full_2 hs_full_3 hs_full_4 hs_full_5 hs_full_6, item casewise asis gen(hs6_full)
alpha hs_forward* hs_reverse*, item casewise asis gen(hs6_half)
egen HS6=rowfirst(hs6_full hs6_half)


** comparing to non-AD measures **

*Trait aggression
corr TraitAgg Pop_for Pop_rev 
corr TraitAgg HS_for HS_rev 

*average aggression against politicians
recode *agg* (1=5) (2=4) (4=2) (5=1)
egen agg_mean=rowmean(*agg*)

corr agg_mean Pop_for Pop_rev
corr agg_mean HS_for HS_rev

*partisan social identity
recode demidentity* repidentity* (1=4) (2=3) (3=2) (4=1)
alpha demident*, item casewise gen(demID)
alpha repident*, item casewise gen(repID)
egen socialpid=rowfirst(demID repID)

corr socialpid Pop_for Pop_rev 
corr socialpid HS_for HS_rev 


** bootstrap correlations **

gen A_Pf=.
gen A_Pr=.

gen C_Pf=.
gen C_Pr=.

gen A_Hf=.
gen A_Hr=.

gen C_Hf=.
gen C_Hr=.

gen Pf_Hf=.
gen Pf_Hr=.
gen Pr_Hf=.
gen Pr_Hr=.


set seed 77007
forval x=1/1000 {
preserve
bsample

qui cor Antidem Pop_for
local APf=r(rho) in `x'
qui cor Antidem Pop_rev
local APr=r(rho) in `x'

qui cor Conspre Pop_for
local CPf=r(rho) in `x'
qui cor Conspre Pop_rev
local CPr=r(rho) in `x'

qui cor Antidem HS_for
local AHf=r(rho) in `x'
qui cor Antidem HS_rev
local AHr=r(rho) in `x'

qui cor Conspre HS_for
local CHf=r(rho) in `x'
qui cor Conspre HS_rev
local CHr=r(rho) in `x'

qui cor Pop_for HS_for
local PfHf=r(rho) in `x'
qui cor Pop_for HS_rev
local PfHr=r(rho) in `x'
qui cor Pop_rev HS_for
local PrHf=r(rho) in `x'
qui cor Pop_rev HS_rev
local PrHr=r(rho) in `x'

restore

qui replace A_Pf=`APf' in `x'
qui replace A_Pr=`APr' in `x'

qui replace C_Pf=`CPf' in `x'
qui replace C_Pr=`CPr' in `x'

qui replace A_Hf=`AHf' in `x'
qui replace A_Hr=`AHr' in `x'

qui replace C_Hf=`CHf' in `x'
qui replace C_Hr=`CHr' in `x'

qui replace Pf_Hf=`PfHf' in `x'
qui replace Pf_Hr=`PfHr' in `x'
qui replace Pr_Hf=`PrHf' in `x'
qui replace Pr_Hr=`PrHr' in `x'


di `x'
}

gen A_Pop_diff=A_Pf-A_Pr
gen C_Pop_diff=C_Pf-C_Pr

gen A_HS_diff=A_Hf-A_Hr
gen C_HS_diff=C_Hf-C_Hr

gen P_HS_fffr_diff=Pf_Hf-Pf_Hr
gen P_HS_ffrf_diff=Pf_Hf-Pr_Hf

gen P_HS_rrfr_diff=Pr_Hr-Pf_Hr
gen P_HS_rrrf_diff=Pr_Hr-Pr_Hf

gen cor1=.
gen cor1se=.
gen cor2=.
gen cor2se=.

gen diff=.
gen se=.

gen cor1var=""
gen cor2var=""

sum A_Pf
replace cor1=r(mean) in 1
replace cor1se=r(sd) in 1
sum A_Pr
replace cor2=r(mean) in 1
replace cor2se=r(mean) in 1

replace cor1var="Af_Pf" in 1
replace cor2var="Af_Pr" in 1

sum A_Pop_diff
replace diff=r(mean) in 1
replace se=r(sd) in 1

sum C_Pf
replace cor1=r(mean) in 2
replace cor1se=r(sd) in 2
sum C_Pr
replace cor2=r(mean) in 2
replace cor2se=r(mean) in 2

replace cor1var="Cf_Pf" in 2
replace cor2var="Cf_Pr" in 2

sum C_Pop_diff
replace diff=r(mean) in 2
replace se=r(sd) in 2

sum A_Hf
replace cor1=r(mean) in 3
replace cor1se=r(sd) in 3
sum A_Hr
replace cor2=r(mean) in 3
replace cor2se=r(mean) in 3

replace cor1var="Af_Sf" in 3
replace cor2var="Af_Sr" in 3

sum A_HS_diff
replace diff=r(mean) in 3
replace se=r(sd) in 3

sum C_Hf
replace cor1=r(mean) in 4
replace cor1se=r(sd) in 4
sum C_Hr
replace cor2=r(mean) in 4
replace cor2se=r(mean) in 4

replace cor1var="Cf_Sf" in 4
replace cor2var="Cf_Sr" in 4

sum C_HS_diff
replace diff=r(mean) in 4
replace se=r(sd) in 4

*gen P_HS_fffr_diff=Pf_Hf-Pf_Hr
*gen P_HS_ffrf_diff=Pf_Hf-Pr_Hf

*gen P_HS_rrfr_diff=Pr_Hr-Pf_Hr
*gen P_HS_rrrf_diff=Pr_Hr-Pr_Hf

sum Pf_Hf
replace cor1=r(mean) in 5
replace cor1se=r(sd) in 5
sum Pf_Hr
replace cor2=r(mean) in 5
replace cor2se=r(mean) in 5

replace cor1var="Pf_Sf" in 5
replace cor2var="Pf_Sr" in 5

sum P_HS_fffr_diff
replace diff=r(mean) in 5
replace se=r(sd) in 5

sum Pf_Hf
replace cor1=r(mean) in 6
replace cor1se=r(sd) in 6
sum Pr_Hf
replace cor2=r(mean) in 6
replace cor2se=r(mean) in 6

replace cor1var="Pf_Sf" in 6
replace cor2var="Pr_Sf" in 6

sum P_HS_ffrf_diff
replace diff=r(mean) in 6
replace se=r(sd) in 6

sum Pr_Hr
replace cor1=r(mean) in 7
replace cor1se=r(sd) in 7
sum Pf_Hr
replace cor2=r(mean) in 7
replace cor2se=r(mean) in 7

replace cor1var="Pr_Sr" in 7
replace cor2var="Pf_Sr" in 7

sum P_HS_rrfr_diff
replace diff=r(mean) in 7
replace se=r(sd) in 7

sum Pr_Hr
replace cor1=r(mean) in 8
replace cor1se=r(sd) in 8
sum Pr_Hf
replace cor2=r(mean) in 8
replace cor2se=r(mean) in 8

replace cor1var="Pr_Sr" in 8
replace cor2var="Pr_Sf" in 8

sum P_HS_rrrf_diff
replace diff=r(mean) in 8
replace se=r(sd) in 8

gen var_pop=0
gen var_anti=0
gen var_consp=0
gen var_hs=0

replace var_pop=1 if (_n==1 | _n==2 | _n==5 | _n==6 | _n==7 | _n==8)
replace var_hs=1 if (_n>2)
replace var_anti=1 if (_n==1 | _n==3)
replace var_consp=1 if (_n==2 | _n==4)

recode var_* (0=.) if _n>8
recode var_* (1=.) if _n>8


gen x=_n
replace x=. if _n>8

gen lo=diff-1.96*se
gen hi=diff+1.96*se

keep diff se lo hi x var_* cor1 cor2 cor1se cor2se cor1var cor2var
drop if _n>8

save "study2_cors.dta", replace